package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.domain.Util;
import org.light.utils.WriteableUtil;

public class ErrorGoGenerator {
	protected String fileName = "error.go";

	public String generateString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package errno"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import \"fmt\""));
		sList.add(new Statement(4000L,0,""));
		sList.add(new Statement(5000L,0,"type Errno struct {"));
		sList.add(new Statement(6000L,1,"Code    int"));
		sList.add(new Statement(7000L,1,"Message string"));
		sList.add(new Statement(8000L,0,"}"));
		sList.add(new Statement(9000L,0,""));
		sList.add(new Statement(10000L,0,"func (err Errno) Error() string {"));
		sList.add(new Statement(11000L,1,"return err.Message"));
		sList.add(new Statement(12000L,0,"}"));
		sList.add(new Statement(13000L,0,""));
		sList.add(new Statement(14000L,0,"// Err represents an error"));
		sList.add(new Statement(15000L,0,"type Err struct {"));
		sList.add(new Statement(16000L,1,"Code    int"));
		sList.add(new Statement(17000L,1,"Message string"));
		sList.add(new Statement(18000L,1,"Err     error"));
		sList.add(new Statement(19000L,0,"}"));
		sList.add(new Statement(20000L,0,""));
		sList.add(new Statement(21000L,0,"func New(errno *Errno, err error) *Err {"));
		sList.add(new Statement(22000L,1,"return &Err{Code: errno.Code, Message: errno.Message, Err: err}"));
		sList.add(new Statement(23000L,0,"}"));
		sList.add(new Statement(24000L,0,""));
		sList.add(new Statement(25000L,0,"func (err *Err) Add(message string) error {"));
		sList.add(new Statement(26000L,1,"err.Message += \" \" + message"));
		sList.add(new Statement(27000L,1,"return err"));
		sList.add(new Statement(28000L,0,"}"));
		sList.add(new Statement(29000L,0,""));
		sList.add(new Statement(30000L,0,"func (err *Err) Addf(format string, args ...interface{}) error {"));
		sList.add(new Statement(31000L,1,"err.Message += \" \" + fmt.Sprintf(format, args...)"));
		sList.add(new Statement(32000L,1,"return err"));
		sList.add(new Statement(33000L,0,"}"));
		sList.add(new Statement(34000L,0,""));
		sList.add(new Statement(35000L,0,"func (err *Err) Error() string {"));
		sList.add(new Statement(36000L,1,"return fmt.Sprintf(\"Err - code: %d, message: %s, error: %s\", err.Code, err.Message, err.Err)"));
		sList.add(new Statement(37000L,0,"}"));
		sList.add(new Statement(38000L,0,""));
		sList.add(new Statement(39000L,0,"func IsErrUserNotFound(err error) bool {"));
		sList.add(new Statement(40000L,1,"code, _ := DecodeErr(err)"));
		sList.add(new Statement(41000L,1,"return code == ErrUserNotFound.Code"));
		sList.add(new Statement(42000L,0,"}"));
		sList.add(new Statement(43000L,0,""));
		sList.add(new Statement(44000L,0,"func DecodeErr(err error) (int, string) {"));
		sList.add(new Statement(45000L,1,"if err == nil {"));
		sList.add(new Statement(46000L,2,"return OK.Code, OK.Message"));
		sList.add(new Statement(47000L,1,"}"));
		sList.add(new Statement(48000L,0,""));
		sList.add(new Statement(49000L,1,"switch typed := err.(type) {"));
		sList.add(new Statement(50000L,1,"case *Err:"));
		sList.add(new Statement(51000L,2,"return typed.Code, typed.Message"));
		sList.add(new Statement(52000L,1,"case *Errno:"));
		sList.add(new Statement(53000L,2,"return typed.Code, typed.Message"));
		sList.add(new Statement(54000L,1,"default:"));
		sList.add(new Statement(55000L,1,"}"));
		sList.add(new Statement(56000L,0,""));
		sList.add(new Statement(57000L,1,"return InternalServerError.Code, err.Error()"));
		sList.add(new Statement(58000L,0,"}"));
		return WriteableUtil.merge(sList).getContent();
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

}
